package book;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Book implements Comparable<Book>{
    private int bookId;//书号；
    private String title;//书名；
    private String author;//作者；
    private String category;//类别；
    private int publishYear;//出版年份；
    private boolean isBorrowed;//借阅状态；
    private int borrowCount;//借阅次数；
    private LocalDate shelfDate;//上架时间；
    //构造方法中的初始化图书对象 书籍ID、借阅状态和 借阅次数不⽤进⾏参数传递


    public Book(String title, String author, String category, int publishYear, LocalDate shelfDate) {
        this.title = title;
        this.author = author;
        this.category = category;
        this.isBorrowed=false;//默认没有被借出；
        this.borrowCount=0;//默认借阅次数为0；
        this.publishYear = publishYear;
        this.shelfDate = shelfDate;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookId=" + bookId +
                ", 书名：'" + title + '\'' +
                ", 作者：'" + author + '\'' +
                ", 类别：'" + category + '\'' +
                ", 出版年份：" + publishYear +
                ", 是否被借出：" + ((isBorrowed) ? "借出了" : "没有借出") +
                ", 借阅次数：" + borrowCount +
                ", 上架日期：" + shelfDate +
                '}';
    }
    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public int getPublishYear() {
        return publishYear;
    }

    public void setPublishYear(int publishYear) {
        this.publishYear = publishYear;
    }

    public LocalDate getShelfDate() {
        return shelfDate;
    }

    public void setShelfDate(LocalDate shelfDate) {
        this.shelfDate = shelfDate;
    }

    public int getBorrowCount() {
        return borrowCount;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }
    public void incrementBorrowCount() {
        this.borrowCount++;
    }
    public void decreaseBorrowCount(){
        this.borrowCount--;
    }

    public void setBorrowCount(int borrowCount) {
        this.borrowCount = borrowCount;
    }

    @Override
    public int compareTo(Book o) {
        return o.getBorrowCount()-this.getBorrowCount();
    }
    public String toJSON() {
        StringBuilder json = new StringBuilder();
        json.append(bookId).append(",");
        json.append(title).append(",");
        json.append(author).append(",");
        json.append(category).append(",");
        json.append(publishYear).append(",");
        json.append(isBorrowed).append(",");
        json.append(borrowCount).append(",");
        json.append(shelfDate != null ?
                shelfDate.format(DateTimeFormatter.ISO_LOCAL_DATE) : "null");
        return json.toString();
    }
}
